package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AddAlertsActivity extends AppCompatActivity {

    private TextView feedback_title;
    private TextView feedback_content;
    private Button feedback_tijiao;
    private ImageView feedback_back;
    private Spinner feedback_NoticeName;
    private List<String> NoticeNamelist =new ArrayList<String>();
    private List<Integer> NoticeIdlist =new ArrayList<Integer>();
    private int SelectNoticeid;
    private CheckBox feedback_check;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_alerts);
        Intent intent = getIntent();
        String PositionId = intent.getStringExtra("PositionId");
        initid();

        initspinner();

        feedback_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        feedback_tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (PositionId.equals("3")){
                    tijiao(Sto.teachers.getWorkerId());
                }else {
                    tijiao(Sto.dept.getWorkerId());
                }
            }
        });

        feedback_NoticeName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                SelectNoticeid = NoticeIdlist.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    private void initid(){
        feedback_back = (ImageView)findViewById(R.id.feedback_back);
        feedback_title = (TextView)findViewById(R.id.feedback_title);
        feedback_content = (TextView)findViewById(R.id.feedback_content);
        feedback_tijiao = (Button) findViewById(R.id.feedback_tijiao);
        feedback_NoticeName = (Spinner) findViewById(R.id.feedback_NoticeName);
        feedback_check = (CheckBox) findViewById(R.id.feedback_check);
    }

    private void tijiao(String workerid){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("AlertsTitle",feedback_title.getText().toString());
                    jsonObject.put("AlertsContent",feedback_content.getText().toString());
                    jsonObject.put("NoticeId",SelectNoticeid);
                    jsonObject.put("workerId",workerid);
                    int a;
                    if (feedback_check.isChecked()){
                        a=1;
                    }else {
                        a=0;
                    }
                    jsonObject.put("StudentShow",a);
                    jsonObject.put("AlterToApplyForTime","");
                    MediaType type = MediaType.parse("application/json;charset=utf-8");
                    final RequestBody requestBody = RequestBody.create(type,""+jsonObject.toString());
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/Alerts/InsertAlerts")
                            .post(requestBody)
                            .build();
                    Response response = client.newCall(request1).execute();
                    String data = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(data);
                    String code = jsonObject1.getString("code");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (code.equals("200")){
                                Toast.makeText(AddAlertsActivity.this,"添加成功",Toast.LENGTH_SHORT).show();
                                feedback_title.setText("");
                                feedback_content.setText("");
                                feedback_check.setChecked(false);
                            }else {
                                Toast.makeText(AddAlertsActivity.this,"添加失败",Toast.LENGTH_SHORT).show();
                            }
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initspinner(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request1 = new Request.Builder()
                            .url(Sto.NetIP+"/api/Notice/GetAllNotice")
                            .get()
                            .build();
                    Response response = client.newCall(request1).execute();
                    String results = response.body().string();
                    JSONObject jsonObject1 = new JSONObject(results);
                    String message = jsonObject1.getString("message");
                    JSONArray jsonArray = new JSONArray(message);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        String NoticeName = jsonObject.getString("NoticeName");
                        int NoticeId = jsonObject.getInt("NoticeId");
                        NoticeNamelist.add(NoticeName);
                        NoticeIdlist.add(NoticeId);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddAlertsActivity.this,R.layout.classid_item, R.id.text,NoticeNamelist);
                            feedback_NoticeName.setAdapter(adapter);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}